<template>
  <div class="app-container">
    <div class="card-box">
      <el-space>
        <el-button type="primary" @click="handleToAdd">添加快递公司</el-button>
      </el-space>
    </div>
    <div class="card-box">
      <el-table v-loading="expressCompanyStore.loading.list" :data="expressCompanyStore.list">
        <el-table-column label="ID" prop="id" />
        <el-table-column label="公司名称" prop="name" />
        <el-table-column label="公司代码" prop="code" />
        <el-table-column label="类型">
          <template #default="scope">
            <el-tag v-if="scope.row.type === 'ems'" type="danger">国际邮政</el-tag>
            <el-tag v-if="scope.row.type === 'domestic'" type="success">国内运输商</el-tag>
            <el-tag v-if="scope.row.type === 'international'" type="primary">国际运输商</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="is_active">
          <template #default="scope">
            <el-tag v-if="scope.row.is_active" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" link @click="handleToEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" placement="top" @confirm="handleDelete(scope.row)">
              <el-button type="danger" link>删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="expressCompanyStore.list.length > 0"
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        background
        style="margin-top: 10px"
        :total="expressCompanyStore.totalCount"
        @change="expressCompanyStore.fetchList(params)"
      />
    </div>
    <!-- 添加表单组件 -->
    <Form v-model="isFormVisible" :mode="formMode" :company="selectedCompany" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import {
  ExpressCompanyListQuery,
  ExpressCompanyType,
  ExpressCompanyForm,
} from "@/api/logistics/company";
import { useExpressCompanyStoreHook } from "@/store/modules/logistics/company";
// 引入表单组件
import Form from "./components/form.vue";

const expressCompanyStore = useExpressCompanyStoreHook();
const params = reactive<ExpressCompanyListQuery>({
  page: 1,
  size: 10,
});
// 控制表单对话框的显示与隐藏
const isFormVisible = ref(false);
// 表单模式
const formMode = ref("add");
// 选中的公司
const selectedCompany = ref<ExpressCompanyForm>({
  id: undefined,
  name: "",
  code: "",
  type: "international",
  is_active: true,
});

function handleToAdd() {
  formMode.value = "add";
  selectedCompany.value = {
    id: undefined,
    name: "",
    code: "",
    type: "international",
    is_active: true,
  };
  // 显示表单对话框
  isFormVisible.value = true;
}

function handleToEdit(row: ExpressCompanyType) {
  formMode.value = "edit";
  selectedCompany.value = row;
  // 显示表单对话框
  isFormVisible.value = true;
}

async function handleDelete(row: ExpressCompanyType) {
  await expressCompanyStore.fetchDelete(row.id);
  await expressCompanyStore.fetchList(params);
}

onMounted(() => {
  expressCompanyStore.fetchList(params);
});
</script>

<style lang="scss" scoped></style>
